{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.19 \u5728\u5b9a\u4e49\u7684\u65f6\u5019\u521d\u59cb\u5316\u7c7b\u7684\u6210\u5458\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u7c7b\u88ab\u5b9a\u4e49\u7684\u65f6\u5019\u5c31\u521d\u59cb\u5316\u4e00\u90e8\u5206\u7c7b\u7684\u6210\u5458\uff0c\u800c\u4e0d\u662f\u8981\u7b49\u5230\u5b9e\u4f8b\u88ab\u521b\u5efa\u540e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7c7b\u5b9a\u4e49\u65f6\u5c31\u6267\u884c\u521d\u59cb\u5316\u6216\u8bbe\u7f6e\u64cd\u4f5c\u662f\u5143\u7c7b\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u573a\u666f\u3002\u672c\u8d28\u4e0a\u8bb2\uff0c\u4e00\u4e2a\u5143\u7c7b\u4f1a\u5728\u5b9a\u4e49\u65f6\u88ab\u89e6\u53d1\uff0c\n\u8fd9\u65f6\u5019\u4f60\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5229\u7528\u8fd9\u4e2a\u601d\u8def\u6765\u521b\u5efa\u7c7b\u4f3c\u4e8e collections \u6a21\u5757\u4e2d\u7684\u547d\u540d\u5143\u7ec4\u7684\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import operator\n\nclass StructTupleMeta(type):\n    def __init__(cls, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        for n, name in enumerate(cls._fields):\n            setattr(cls, name, property(operator.itemgetter(n)))\n\nclass StructTuple(tuple, metaclass=StructTupleMeta):\n    _fields = []\n    def __new__(cls, *args):\n        if len(args) != len(cls._fields):\n            raise ValueError('{} arguments required'.format(len(cls._fields)))\n        return super().__new__(cls,args)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u7b80\u5355\u7684\u57fa\u4e8e\u5143\u7ec4\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Stock(StructTuple):\n    _fields = ['name', 'shares', 'price']\n\nclass Point(StructTuple):\n    _fields = ['x', 'y']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u6f14\u793a\u5b83\u5982\u4f55\u5de5\u4f5c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = Stock('ACME', 50, 91.1)\ns"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.name"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.shares * s.price"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.shares = 23"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u4e2d\uff0c\u7c7b StructTupleMeta \u83b7\u53d6\u5230\u7c7b\u5c5e\u6027 _fields \u4e2d\u7684\u5c5e\u6027\u540d\u5b57\u5217\u8868\uff0c\n\u7136\u540e\u5c06\u5b83\u4eec\u8f6c\u6362\u6210\u76f8\u5e94\u7684\u53ef\u8bbf\u95ee\u7279\u5b9a\u5143\u7ec4\u69fd\u7684\u65b9\u6cd5\u3002\u51fd\u6570 operator.itemgetter() \u521b\u5efa\u4e00\u4e2a\u8bbf\u95ee\u5668\u51fd\u6570\uff0c\n\u7136\u540e property() \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u5c5e\u6027\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6700\u96be\u61c2\u7684\u90e8\u5206\u662f\u77e5\u9053\u4e0d\u540c\u7684\u521d\u59cb\u5316\u6b65\u9aa4\u662f\u4ec0\u4e48\u65f6\u5019\u53d1\u751f\u7684\u3002\nStructTupleMeta \u4e2d\u7684 __init__() \u65b9\u6cd5\u53ea\u5728\u6bcf\u4e2a\u7c7b\u88ab\u5b9a\u4e49\u65f6\u88ab\u8c03\u7528\u4e00\u6b21\u3002\ncls \u53c2\u6570\u5c31\u662f\u90a3\u4e2a\u88ab\u5b9a\u4e49\u7684\u7c7b\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528\u4e86 _fields \u7c7b\u53d8\u91cf\u6765\u4fdd\u5b58\u65b0\u7684\u88ab\u5b9a\u4e49\u7684\u7c7b\uff0c\n\u7136\u540e\u7ed9\u5b83\u518d\u6dfb\u52a0\u4e00\u70b9\u65b0\u7684\u4e1c\u897f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "StructTuple \u7c7b\u4f5c\u4e3a\u4e00\u4e2a\u666e\u901a\u7684\u57fa\u7c7b\uff0c\u4f9b\u5176\u4ed6\u4f7f\u7528\u8005\u6765\u7ee7\u627f\u3002\n\u8fd9\u4e2a\u7c7b\u4e2d\u7684 __new__() \u65b9\u6cd5\u7528\u6765\u6784\u9020\u65b0\u7684\u5b9e\u4f8b\u3002\n\u8fd9\u91cc\u4f7f\u7528 __new__() \u5e76\u4e0d\u662f\u5f88\u5e38\u89c1\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u6211\u4eec\u8981\u4fee\u6539\u5143\u7ec4\u7684\u8c03\u7528\u7b7e\u540d\uff0c\n\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u50cf\u666e\u901a\u7684\u5b9e\u4f8b\u8c03\u7528\u90a3\u6837\u521b\u5efa\u5b9e\u4f8b\u3002\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = Stock('ACME', 50, 91.1) # OK\ns = Stock(('ACME', 50, 91.1)) # Error"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8ddf __init__() \u4e0d\u540c\u7684\u662f\uff0c__new__() \u65b9\u6cd5\u5728\u5b9e\u4f8b\u88ab\u521b\u5efa\u4e4b\u524d\u88ab\u89e6\u53d1\u3002\n\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u4fee\u6539\u7684\uff0c\u6240\u4ee5\u4e00\u65e6\u5b83\u4eec\u88ab\u521b\u5efa\u4e86\u5c31\u4e0d\u53ef\u80fd\u5bf9\u5b83\u505a\u4efb\u4f55\u6539\u53d8\u3002\u800c __init__() \u4f1a\u5728\u5b9e\u4f8b\u521b\u5efa\u7684\u6700\u540e\u88ab\u89e6\u53d1\uff0c\n\u8fd9\u6837\u7684\u8bdd\u6211\u4eec\u5c31\u53ef\u4ee5\u505a\u6211\u4eec\u60f3\u505a\u7684\u4e86\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 __new__() \u65b9\u6cd5\u5df2\u7ecf\u88ab\u5b9a\u4e49\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u672c\u8282\u5f88\u77ed\uff0c\u8fd8\u662f\u9700\u8981\u4f60\u80fd\u4ed4\u7ec6\u7814\u8bfb\uff0c\u6df1\u5165\u601d\u8003Python\u7c7b\u662f\u5982\u4f55\u88ab\u5b9a\u4e49\u7684\uff0c\u5b9e\u4f8b\u662f\u5982\u4f55\u88ab\u521b\u5efa\u7684\uff0c\n\u8fd8\u6709\u5c31\u662f\u5143\u7c7b\u548c\u7c7b\u7684\u5404\u4e2a\u4e0d\u540c\u7684\u65b9\u6cd5\u7a76\u7adf\u5728\u4ec0\u4e48\u65f6\u5019\u88ab\u8c03\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "PEP 422\n\u63d0\u4f9b\u4e86\u4e00\u4e2a\u89e3\u51b3\u672c\u8282\u95ee\u9898\u7684\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u3002\n\u4f46\u662f\uff0c\u622a\u6b62\u5230\u6211\u5199\u8fd9\u672c\u4e66\u7684\u65f6\u5019\uff0c\u5b83\u8fd8\u6ca1\u88ab\u91c7\u7eb3\u548c\u63a5\u53d7\u3002\n\u5c3d\u7ba1\u5982\u6b64\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPython 3.3\u6216\u66f4\u9ad8\u7684\u7248\u672c\uff0c\u90a3\u4e48\u8fd8\u662f\u503c\u5f97\u53bb\u770b\u4e00\u4e0b\u7684\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}